<%--
  Created by IntelliJ IDEA.
  User: dongp
  Date: 2016/5/8
  Time: 10:33
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <div class="container">
            <div class="row" style="position: relative">
                <div class="col-xs-3" style="padding-left: 0px;">
                    <form id="searchForm" method="post">
                        <div class="form-group">
                            <label for="search" class="sr-only">搜索</label>
                            <input type="text" class="form-control" id="search" name="search" placeholder="搜索..." >
                        </div>
                    </form>
                </div>
                <span class=" glyphicon glyphicon-search" id="searchImg" style="position: absolute;left: 250px;top: 10px;"></span>
            </div>

        <c:forEach items="${demandList}" var="demand">
            <div class="row content">
               <ul>
                   <li><a href="/web/demand/queryById?id=${demand.getId()}">${demand.getName()}</a></li>
                   <li>
                       <p class="col-xs-4">行业分类：${demand.getTypeName()}</p>
                       <p class="col-xs-4">所属单位：${demand.getOrganization()}</p>
                       <p class="col-xs-4">预算：${demand.getbudget()}</p>
                   </li>
               </ul>
            </div>
        </c:forEach>

        <%--我们需要计算页码列表的开始和结束位置，即两个变量begin和end
        计算它们需要通过当前页码！
        1. 总页数不足6页--> begin=1, end=最大页
        2. 通过公式设置begin和end，begin=当前页-1，end=当前页+3
        3. 如果begin<1，那么让begin=1，end=6
        4. 如果end>tp, 让begin=tp-5, end=tp
         --%>
        <c:choose>
            <c:when test="${pageBean.getTotalPage() <= 6 }">
                <c:set var="begin" value="1"/>
                <c:set var="end" value="${pageBean.getTotalPage() }"/>
            </c:when>
            <c:otherwise>
                <c:set var="begin" value="${pageBean.getPageOffset()-2 }"/>
                <c:set var="end" value="${pageBean.getPageOffset() + 3}"/>
                <c:if test="${begin < 1 }">
                    <c:set var="begin" value="1"/>
                    <c:set var="end" value="6"/>
                </c:if>
                <c:if test="${end > pageBean.getTotalPage() }">
                    <c:set var="begin" value="${pageBean.getTotalPage()-5 }"/>
                    <c:set var="end" value="${pageBean.getTotalPage() }"/>
                </c:if>
            </c:otherwise>
        </c:choose>


        <%-- 计算begin和end --%>
        <%-- 如果总页数<=6，那么显示所有页码，即begin=1 end=${pb.tp} --%>
        <%-- 设置begin=当前页码-2，end=当前页码+3 --%>
        <%-- 如果begin<1，那么让begin=1 end=6 --%>
        <%-- 如果end>最大页，那么begin=最大页-5 end=最大页 --%>
        <span class="paginate">总页数
            <c:choose>
                <c:when test="${pageBean.getTotalPage() == 0}">
                    1
                </c:when>
                <c:otherwise>
                    ${pageBean.getTotalPage()}
                </c:otherwise>
            </c:choose>
            页,&nbsp;现在是第${pageBean.getPageOffset()}页
        </span>
        <ul class="pagination" style="float: right">
            <c:choose>
                <c:when test="${pageBean.getPageOffset() eq 1}">
                    <li class="disabled">
                        <a href="#" aria-label="Previous">
                            <span aria-hidden="true">&laquo;</span>
                        </a>
                    </li>
                </c:when>
                <c:otherwise >
                    <li>
                        <a href="/web/demand/queryPage?page=${pageBean.getPageOffset()-1}" aria-label="Previous">
                            <span aria-hidden="true">&laquo;</span>
                        </a>
                    </li>
                </c:otherwise>
            </c:choose>

            <c:forEach begin="${begin }" end="${end }" var="i">
                <c:choose>
                    <c:when test="${i eq pageBean.getPageOffset() }">
                        <li class="active"><a href="/web/demand/queryPage?page=${i}">${i}</a></li>
                    </c:when>
                    <c:otherwise>
                        <li><a href="/web/demand/queryPage?page=${i}">${i }</a></li>
                    </c:otherwise>
                </c:choose>
            </c:forEach>
            <%-- 显示点点点 --%>
            <c:if test="${end < pageBean.getTotalPage() }">
                <li><a href="#">...</a></li>
            </c:if>

            <c:choose>
                <c:when test="${pageBean.getPageOffset() eq pageBean.getTotalPage()}">
                    <li class="disabled">
                        <a href="#" aria-label="Next">
                            <span aria-hidden="true">&raquo;</span>
                        </a>
                    </li>
                </c:when>
                <c:otherwise >
                    <li>
                        <a href="/web/demand/queryPage?page=${pageBean.getPageOffset()+1}" aria-label="Next">
                            <span aria-hidden="true">&raquo;</span>
                        </a>
                    </li>
                </c:otherwise>
            </c:choose>
        </ul>

    </div>
